local function translateNumber(num)
    local chineseNum = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
    local units = {"", "十", "百", "千", "万"}
    if num == 0 then
        return chineseNum[1]
    end
    local result = ""
    local unitIndex = 1
    local lastNonZero = false
    while num > 0 do
        local digit = num % 10
        if digit ~= 0 then
            result = chineseNum[digit + 1] .. units[unitIndex] .. result
            lastNonZero = true
        elseif lastNonZero then
            result = chineseNum[digit + 1] .. result
            lastNonZero = false
        end
 
        unitIndex = unitIndex + 1
        num = math.floor(num / 10)
    end
    if string.sub(result, 1, 6) == "一十" then
        result = string.sub(result, 4)
    end
    return result
end
local jingxian = fk.CreateSkill{
    name = "ym1__jingxian",
    tags = {Skill.Compulsory},
    dynamic_desc = function (self, player, lang)
        local mark = type(player:getMark("@[ym1__jingxian]")) == "table" and player:getMark("@[ym1__jingxian]") or {2,0}
        return "锁定技，你使用本回合未被使用过的花色时，摸"..translateNumber(mark[1]).."张牌再弃置"..translateNumber(mark[2]).."张手牌，然后弃置牌数+1，若因此失去所有手牌，交换弃牌数与摸牌数。"
    end,
}
Fk:loadTranslationTable{
    ["ym1__jingxian"] = "惊弦",
    [":ym1__jingxian"] = "锁定技，你使用本回合未被使用过的花色时，摸两张牌再弃置〇张手牌，然后弃置牌数+1，若因此失去所有手牌，交换弃牌数与摸牌数。",
    ["@[ym1__jingxian]"] = "惊弦",
    ["ym1__jingxian-turn"] = "",
}
Fk:addQmlMark{
    name = "ym1__jingxian",
    qml_path = "",
    how_to_show = function (name, value, player)
        local numbers = {}
        if type(value) ~= "table" then numbers = {2,0} else numbers = table.simpleClone(value) end
        local str =  numbers[1].." "..numbers[2]
        local suits = player:getTableMark("ym1__jingxian-turn")
        return str.." "..table.concat(table.map(suits,Util.TranslateMapper))
    end,
}
jingxian:addEffect(fk.CardUsing,{
    anim_type = "drawcard",
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(jingxian.name) and target == player then
            local suits = {}
            player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
                local card = e.data.card
                if e.data ~= data then
                    table.insertIfNeed(suits, card.suit)
                end
            end, Player.HistoryTurn)
            return not table.contains(suits,data.card.suit)
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local mark = type(player:getMark("@[ym1__jingxian]")) == "table" and player:getMark("@[ym1__jingxian]") or {2,0}
        local number1, number2 = mark[1], mark[2]
        mark[2] = mark[2] + 1
        room:setPlayerMark(player,"@[ym1__jingxian]",mark)
        if number1 > 0 then
            player:drawCards(number1,jingxian.name)
        end
        if number2 > 0 then
            if player:getHandcardNum() > number2 then
                room:askToDiscard(player,{
                    max_num = number2,
                    min_num = number2,
                    skill_name = jingxian.name,
                    include_equip = false,
                    cancelable = false,
                })
            else
                room:throwCard(player:getCardIds("h"),jingxian.name,player,player)
            end
        end
        --[[mark = type(player:getMark("@[ym1__jingxian]")) == "table" and player:getMark("@[ym1__jingxian]") or {2,0}
        room:setPlayerMark(player,"@[ym1__jingxian]",mark)]]--
    end,
})
jingxian:addEffect(fk.AfterCardsMove,{
    can_refresh = function (self, event, target, player, data)
        if not player:isKongcheng() then return end
        for _, move in ipairs(data) do
            p(move.skillName)
            if move.from == player and move.skillName == jingxian.name then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand then
                        return true
                    end
                end
            end
        end
    end,
    on_refresh = function (self, event, target, player, data)
        local mark = type(player:getMark("@[ym1__jingxian]")) == "table" and player:getMark("@[ym1__jingxian]") or {2,0}
        local number1, number2 = mark[1], mark[2]
        mark[1], mark[2] = number2, number1
        player.room:setPlayerMark(player,"@[ym1__jingxian]",mark)
    end,
})
jingxian:addEffect(fk.CardUsing,{
    can_refresh = function (self, event, target, player, data)
        return player:hasSkill(jingxian.name) and not table.contains(player:getTableMark("ym1__jingxian-turn"),data.card:getSuitString(true))
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:addTableMark(player,"ym1__jingxian-turn",data.card:getSuitString(true))
    end,
})
jingxian:addAcquireEffect(function (self, player, is_start)
    player.room:setPlayerMark(player,"@[ym1__jingxian]",{2,0})
end)
jingxian:addLoseEffect(function (self, player, is_death)
    player.room:setPlayerMark(player,"@[ym1__jingxian]",0)
end)
return jingxian